//
//  ChatRoomHotNewsViewController.m
//  suRead
//
//  Created by 师爱华 on 17/2/27.
//  Copyright © 2017年 bao. All rights reserved.
//

#import "ChatRoomHotNewsViewController.h"

#import "ServiceUtils.h"
#import "ServiceNews.h"
#import "Masonry.h"
#import "MJRefresh.h"
#import "UIControl+Blocks.h"
#import "M_News.h"
#import "NewsOneOfImageCell.h"
#import "ZhuanTiCell.h"
#import "NewsAdvCell.h"
#import "NewsCell.h"
#import "NewsImageCell.h"
#import "WenBaTagListController.h"
#import "WenBaDetailController.h"
#import "NewsAlbumViewController.h"
#import "NewsWebViewController.h"
#import "SpecialNewsListViewController.h"
#import "NewsDetailViewController.h"
#import "NewsQuestionCell.h"
#import "ServiceNews.h"

@interface ChatRoomHotNewsViewController () <UITableViewDataSource,UITableViewDelegate,NewsQuestionCellDelegate>

@end

@implementation ChatRoomHotNewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
        //热门推荐区
        if(self.hotTJTableView == nil){
            CGRect contentViewFrame = CGRectMake(0, 0, self.view.bounds.size.width,self.view.bounds.size.height);
            self.hotTJTableView = [[UITableView alloc] initWithFrame:contentViewFrame style:UITableViewStylePlain];
            self.hotTJTableView.delegate = self;
            self.hotTJTableView.dataSource = self;
            self.hotTJTableView.tableFooterView = [UIView new];
            [self.view addSubview:self.hotTJTableView];
    
        }
    __weak ChatRoomHotNewsViewController *weakSelf = self;
    
        [self.hotTJTableView registerNib:[UINib nibWithNibName:@"NewsCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"NewsCell"];
    
    
        self.hotTJTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf refreshData];
        }];
    
        self.hotTJTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [weakSelf loadMore];
        }];
    
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.hotTJTableView.mj_header beginRefreshing];
        });

    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




#pragma mark tableViewDatasouce Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.hotTJNews count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    M_News *news = [self.hotTJNews objectAtIndex:indexPath.row];
    
    if (self.wohs == 1) {
        //特殊新闻
        NewsOneOfImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsOneOfImageCell"];
        cell.newsData = news;
        return cell;
    }
    if (self.wohs == 2) {
        //问吧
        
        if([news.f_type intValue]==1){
            //问吧专题
            ZhuanTiCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZhuanTiCell"];
            cell.newsData = news;
            return cell;
        }else{
            //问吧普通cell
            NewsQuestionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsQuestionCell"];
            cell.newsData = news;
            cell.newsQuestionCellDelegate=self;
            return cell;
        }
        
    }
    
    
    if ([news.f_commentsUrl rangeOfString:@"type=adv"].location != NSNotFound) {
        //3张图片的新闻
        NewsAdvCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsAdvCell"];
        cell.newsData = news;
        return cell;
    }
    
    if ([news.f_type isEqualToString:@"doc"]) {
        //普通新闻，一张图，标题
        NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsCell"];
        cell.newsData = news;
        return cell;
    }
    else if([news.f_type isEqualToString:@"slide"]) {
        //3张图片的新闻
        NewsImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsImageCell"];
        cell.newsData = news;
        return cell;
    }
    else {
        //其他，待定
        NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsCell"];
        cell.newsData = news;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    M_News *news = [self.hotTJNews objectAtIndex:indexPath.row];
    
    
    if (self.wohs == 1||self.wohs == 2) {
        //特殊新闻
        return 251.0f;
    }
    
    if ([news.f_type isEqualToString:@"doc"]) {
        //普通新闻，一张图，标题
        return 100.0f;
    }
    else if([news.f_type isEqualToString:@"slide"]) {
        //3张图片的新闻
        return 155.0f;
    }
    else {
        //其他，待定
        return 0;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    M_News *news_ = [self.hotTJNews objectAtIndex:indexPath.row];
    [self jumpToNewsDetail:news_];
}


- (void)refreshData
{
    self.currentPageIndex = 1;
    if (self.requestHandle) {
        [self.requestHandle cancel];
        self.requestHandle = nil;
    }
    NSString *subURL=@"http://www3.ctdsb.net/index.php?m=live&c=index&a=postdata";
    
    __weak typeof(self) weak_self = self;
    [ServiceNews doHotNewsList:subURL pageindex:self.currentPageIndex pagesize:DEFAULT_PAGE_SIZE success:^(id responseObject, NSString *message) {
        [weak_self.hotTJTableView.mj_header endRefreshing];
        NSArray *listNews_ = [responseObject objectForKey:@"list"];
        weak_self.hotTJNews = [NSMutableArray array];
        if (listNews_) {
            [weak_self.hotTJNews addObjectsFromArray:listNews_];
        }
        
        
        [weak_self.hotTJTableView reloadData];
        if ([listNews_ count]<DEFAULT_PAGE_SIZE) {
            [weak_self.hotTJTableView.mj_footer endRefreshingWithNoMoreData];
        }
        
    } failure:^(NSString *message) {
        [weak_self.hotTJTableView.mj_header endRefreshing];
    }];
}

- (void)loadMore
{
    self.currentPageIndex ++;
    if (self.requestHandle) {
        [self.requestHandle cancel];
        self.requestHandle = nil;
    }
    NSString *subURL=@"http://www3.ctdsb.net/index.php?m=live&c=index&a=postdata";
    __weak typeof(self) weak_self = self;
    [ServiceNews doHotNewsList:subURL pageindex:self.currentPageIndex pagesize:DEFAULT_PAGE_SIZE success:^(id responseObject, NSString *message) {
        
        [weak_self.hotTJTableView.mj_footer endRefreshing];
        NSArray *listNews_ = [responseObject objectForKey:@"list"];
        if (listNews_) {
            [weak_self.hotTJNews addObjectsFromArray:listNews_];
        }
        
        [weak_self.hotTJTableView reloadData];
        if ([listNews_ count]<DEFAULT_PAGE_SIZE) {
            [weak_self.hotTJTableView.mj_footer endRefreshingWithNoMoreData];
        }
        
    } failure:^(NSString *message) {
        [weak_self.hotTJTableView.mj_footer endRefreshing];
    }];
}



-(void) toWenBaCtgListPage:(NSString *) ctgUrl{
    //去问吧专题界面
    //专题
    WenBaTagListController *controller = [WenBaTagListController new];
    controller.hidesBottomBarWhenPushed = YES;
    controller.subURL =ctgUrl;
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)jumpToNewsDetail:(M_News *)news_
{
    if (self.wohs == 2){
        if([news_.f_type isEqualToString:@"1"]){
            //问吧专题
            [self toWenBaCtgListPage:news_.f_url];
        }else{
            //问吧详情
            WenBaDetailController *controller = [WenBaDetailController new];
            controller.hidesBottomBarWhenPushed = YES;
            controller.subURL=news_.f_url;
            if ( [[[UIDevice currentDevice]systemVersion] floatValue] >= 8.0) {
                [self showViewController:controller sender:self];
            }else{
                [self.navigationController pushViewController:controller animated:YES];
            }
        }
        
    }else {
        [ServiceNews doSendRead:news_.f_id
                        success:^(id responseObject, NSString *message)
         {}
                        failure:^(NSString *message)
         {}];
        
        //3种跳转方式
        if ([news_.f_type isEqualToString:@"slide"]) {
            //图集
            NewsAlbumViewController *controller = [NewsAlbumViewController new];
            controller.hidesBottomBarWhenPushed = YES;
            controller.urlString = news_.f_url;
            controller.f_thumbnail = news_.f_thumbnail;
            if ( [[[UIDevice currentDevice]systemVersion] floatValue] >= 8.0) {
                [self showViewController:controller sender:self];
            }else{
                [self.navigationController pushViewController:controller animated:YES];
            }
        }
        else if([news_.f_type isEqualToString:@"doc"]) {
            if ([news_.f_commentsUrl rangeOfString:@"type=forward"].location != NSNotFound) {
                //外链新闻
                NewsWebViewController *controller = [NewsWebViewController new];
                controller.hidesBottomBarWhenPushed = YES;
                controller.urlString = news_.f_commentsUrl;
                controller.imgUrlString = news_.f_thumbnail;
                controller.titleString = news_.f_title;
                if ([news_.f_commentsUrl rangeOfString:@"share=true"].location != NSNotFound)
                {
                    controller.hasShare = YES;
                }
                [self.navigationController pushViewController:controller animated:YES];
            }
            else if ([news_.f_commentsUrl rangeOfString:@"type=adv"].location != NSNotFound) {
                //广告
                NewsWebViewController *controller = [NewsWebViewController new];
                controller.hidesBottomBarWhenPushed = YES;
                controller.urlString = news_.f_commentsUrl;
                controller.imgUrlString = news_.f_thumbnail;
                controller.titleString = news_.f_title;
                if ([news_.f_commentsUrl rangeOfString:@"share=true"].location != NSNotFound)
                {
                    controller.hasShare = YES;
                }
                [self.navigationController pushViewController:controller animated:YES];
            }
            else if ([news_.f_commentsUrl rangeOfString:@"type=zt"].location != NSNotFound) {
                //专题链接形式
                NewsWebViewController *controller = [NewsWebViewController new];
                controller.hidesBottomBarWhenPushed = YES;
                controller.urlString = news_.f_commentsUrl;
                controller.imgUrlString = news_.f_thumbnail;
                controller.titleString = news_.f_title;
                if ([news_.f_commentsUrl rangeOfString:@"share=true"].location != NSNotFound)
                {
                    controller.hasShare = YES;
                }
                [self.navigationController pushViewController:controller animated:YES];
            }
            else if ([news_.f_commentsUrl rangeOfString:@"specialid="].location != NSNotFound) {
                //专题
                SpecialNewsListViewController *controller = [SpecialNewsListViewController new];
                controller.hidesBottomBarWhenPushed = YES;
                controller.sharethumb=news_.f_thumbnail;
                controller.sharetitle=news_.f_title;
                controller.subURL = [NSString stringWithFormat:@"%@&%@",URL_SUB_GET_SPECIAL_NEWS,[[news_.f_commentsUrl componentsSeparatedByString:@"?"] lastObject]];
                [self.navigationController pushViewController:controller animated:YES];
            }
            else {
                //                if(news_.f_id !=nil) {
                //                    [self loginRongCloud:nil];
                //                }else {
                //普通新闻详情，需要请求详情，拼接HTML
                NewsDetailViewController *controller = [NewsDetailViewController new];
                controller.hidesBottomBarWhenPushed = YES;
                controller.urlString = [NSString stringWithFormat:@"%@&aid=%@",URL_SUB_GET_NEWS_DETAIL,news_.f_id];
                controller.f_thumbnail = news_.f_thumbnail;
                if ( [[[UIDevice currentDevice]systemVersion] floatValue] >= 8.0) {
                    [self showViewController:controller sender:self];
                }else{
                    [self.navigationController pushViewController:controller animated:YES];
                }
                //                }
            }
        }
        else {
            //待定
        }
    }
    
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
